/**
 * 侧边栏js
 * @author zhuqiang
 * @version V1.0
 * @date 2016/7/14
 */
var side = {
    urls: {
        userReadTopArticles: function (account) {
            return "/" + account + "/article/top?cd=readNum";
        },
        userCategorys: function (account) {
            return "/" + account + "/article/categorys";
        },
        topReadArticles: function () {
            return "/article/top?cd=readNum";
        },
        /** 文章详情页 */
        articleDetails: function (account, articleId) {
            return "/" + account + "/article/details/" + articleId + ".html"
        }
    },
    tmpl: {
        userReadTopArticles: function () {
            return '{{if isNoData}}' +
                '{{each articles as article i}}' +
                    '<a href="'+ side.urls.articleDetails("{{article.userAccount}}","{{article.id}}") +'" class="list-group-item" alt="{{article.title}}"' +
                        '"data-toggle="tooltip" data-placement="top" title="{{article.title}}">' +
                        '<span class="text-overflow" style="width: 70%">{{article.title}} </span><span class="badge"> {{article.readNum}}</span> ' +
                    '</a>' +
                '{{/each}}' +
                '{{else}}'+
                    '<a class="list-group-item text-center" >暂无数据</a>'+
                '{{/if}}';
        },
        userCategorys:function(){
            return '{{if isNoData}}' +
                '{{each categorys as category i}}' +
                    '<a href="/{{userAccount}}/article/category/{{category.id}}" class="list-group-item" alt="{{category.name}}"' +
                        '"data-toggle="tooltip" data-placement="top" title="{{category.name}}">' +
                        '<span class="text-overflow" style="width: 70%">{{category.name}} </span><span class="badge"> {{category.num}}</span> ' +
                    '</a>' +
                '{{/each}}' +
                '{{else}}'+
                    '<a class="list-group-item text-center" >暂无数据</a>'+
                '{{/if}}';
        },
        topReadArticles:function(){
            return '{{if isNoData}}' +
                '{{each articles as article i}}' +
                    '<a href="'+ side.urls.articleDetails("{{article.userAccount}}","{{article.id}}") +'" class="list-group-item" alt="{{article.title}}"' +
                    '"data-toggle="tooltip" data-placement="top" title="{{article.title}}">' +
                    '<span class="text-overflow" style="width: 70%">{{article.title}} </span><span class="badge"> {{article.readNum}}</span> ' +
                '</a>' +
                '{{/each}}' +
                '{{else}}'+
                    '<a class="list-group-item text-center" >暂无数据</a>'+
                '{{/if}}';
        }
    },
    /** 用户文章阅读排行 */
    userReadTopArticles: function (account) {
        $.ajax({
            url: side.urls.userReadTopArticles(account),
            type: "GET",
            dataType: 'json',
            success: function (res) {
                Comm.ajax.resultHander(res,function (code,msg,data) {
                    var $userReadTopAtricles = $("#userReadTopAtricles");
                    $userReadTopAtricles.empty();
                    var tmplData = {
                        isNoData:!!data,
                        articles: data
                    };
                    $userReadTopAtricles.html(template.compile(side.tmpl.userReadTopArticles())(tmplData));
                },null,null,null);
            },
            error: function (er) {
                 Comm.ajax.errAlert(er);;
            }
        });
    },
    /** 用户所有的分类列表 */
    userCategorys: function (account) {
        $.ajax({
            url: side.urls.userCategorys(account),
            type: "GET",
            dataType: 'json',
            success: function (res) {
                Comm.ajax.resultHander(res,function (code,msg,data) {
                    var $warp = $("#userCategorys");
                    $warp.empty();
                    var tmplData = {
                        isNoData:!!data,
                        userAccount:account,
                        categorys: data
                    };
                    $warp.html(template.compile(side.tmpl.userCategorys())(tmplData));
                },null,null,null);
            },
            error: function (er) {
                 Comm.ajax.errAlert(er);;
            }
        });

    },
    /** 全站文章阅读排行 */
    topReadArticles: function () {
        $.ajax({
            url: side.urls.topReadArticles(),
            type: "GET",
            dataType: 'json',
            success: function (res) {
                Comm.ajax.resultHander(res,function (code,msg,data) {
                    var $warp = $("#topReadArticles");
                    $warp.empty();
                    var tmplData = {
                        isNoData:!!data,
                        articles: data
                    };
                    $warp.html(template.compile(side.tmpl.topReadArticles())(tmplData));
                },null,null,null);
            },
            error: function (er) {
                 Comm.ajax.errAlert(er);;
            }
        });
    }
}
